<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/19
 * Time: 13:14
 */

namespace Goods\Controller;

use Admin\Common\CommonController;

/**
 * Class ListsController
 * @package Goods\Controller
 * 商品列表
 */
class ListsController extends CommonController
{
//  商品列表
    public function index()
    {
        $cid=I('get.cid');
        if($cid){
            //表示分类搜索
            $count = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where("sp_goods.cid=$cid")
                ->count();
            $Page  = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where("sp_goods.cid=$cid")
                ->order('sp_goods.gid')
               ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        }elseif(IS_POST){
           $gname=I('post.gname');
            $map['gname'] = array('like',"%{$gname}%");
            $count = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where($map)
                ->count();
            $Page  = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where($map)
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
//            dd($gname);die;
        }else{
            //没有条件的查找全部数据
            $count      = M('goods')->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        }
        $this->assign('data', $data);
//        dd($data);
        $this->assign('page',$show);// 赋值分页输出
        //查出分类分类数据
        $category=M('category')->select();
        $this->assign('category', $category);
//        dd($category);
//       dd($data);
        $this->display();
    }
    /**
     * 上架商品列表
     */
    public  function putawaylists(){
        $cid=I('get.cid');
        if($cid){
            //表示分类搜索
            $count      = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where("sp_goods.cid=$cid")
                ->where('sp_goods.status=1')
                ->count();
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where("sp_goods.cid=$cid")
                ->where('sp_goods.status=1')
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        }elseif(IS_POST){
            $gname=I('post.gname');
            $map['gname'] = array('like',"%{$gname}%");
            $count=M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where($map)
                ->where('sp_goods.status=1')
                ->count();
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where($map)
                ->where('sp_goods.status=1')
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
//            dd($gname);die;
        }else{
            //没有条件的查找全部数据
            $count=M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where('sp_goods.status=1')
                ->count();
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where('sp_goods.status=1')
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        }

        $this->assign('data', $data);
        $this->assign('page',$show);// 赋值分页输出
        //查出分类分类数据
        $category=M('category')->select();
        $this->assign('category', $category);
//        dd($category);
//       dd($data);
        $this->display();
    }
    /**
     * 下架商品列表
     */
    public  function putoutlists(){
        $cid=I('get.cid');
        if($cid){
            //表示分类搜索
            $count      = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where("sp_goods.cid=$cid")
                ->where('sp_goods.status=0')
                ->count();
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where("sp_goods.cid=$cid AND　sp_goods.status=0")
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        }elseif(IS_POST){
            $gname=I('post.gname');
            $map['gname'] = array('like',"%{$gname}%");
            $count=M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where($map)
                ->where('sp_goods.status=0')
                ->count();
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where($map)
                ->where('sp_goods.status=0')
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
//            dd($gname);die;
        }else{
            //没有条件的查找全部数据
            $count=M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where('sp_goods.status=0')
                ->count();
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//            $Page->setConfig('header','个会员');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('theme',' 总共%TOTAL_ROW%件商品 %UP_PAGE%  %FIRST%  %LINK_PAGE% %DOWN_PAGE% %END%');
            $show= $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $data = M('goods')
                ->join('sp_category ON sp_goods.cid = sp_category.cid')
                ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
                ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
                ->where('sp_goods.status=0')
                ->order('sp_goods.gid')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        }

        $this->assign('data', $data);
        $this->assign('page',$show);// 赋值分页输出
        //查出分类分类数据
        $category=M('category')->select();
        $this->assign('category', $category);
//        dd($category);
//       dd($data);
        $this->display();
    }

    /**
     * 商品详情
     */
    public function  info()
    {
        $gid = I('gid');
//        超找旧数据
        $info = $data = M('goods')
            ->join('sp_category ON sp_goods.cid = sp_category.cid')
            ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
            ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
            ->where("sp_goods.gid=$gid")
            ->find();
        $info['style'] = json_decode($info['style'], true);
        //找出商品缩略图
        $pic = M('pic')->where("gid=$gid")->getField('pic', true);
        $info['pic'] = $pic;
        $this->assign('info', $info);
//        dd($info);
        $this->display();
    }

    /**
     * 商品编辑
     */
    public function edit()
    {
        $gid = I('get.gid');

//        dd($gid);
//        查出旧数据
        $olddata = $data = M('goods')
            ->join('sp_category ON sp_goods.cid = sp_category.cid')
            ->join('sp_nature ON sp_goods.nid=sp_nature.nid')
            ->join('sp_brand ON sp_goods.bid=sp_brand.bid')
            ->where("sp_goods.gid=$gid")
            ->find();
        //找出商品缩略图
        $pic = M('pic')->where("gid=$gid")->getField('pic', true);
        $olddata['pic'] = $pic;
        $this->assign('olddata', $olddata);
//        dd($olddata);
        //找出所有的种类
        $nature = M('nature')->select();
        $this->assign('nature', $nature);
        //找出所有的品牌
        $brand = M('brand')->select();
        $this->assign('brand', $brand);
        //找出所有的分类(只能级别比他高的分类)
        $son = $this->finson($olddata['cid'], M('category')->select());
        $son[] = $olddata['cid'];
        $map['cid'] = ['not in', $son];
        $category = M('category')->where($map)->select();
        $this->assign('category', $category);
        //商品属性

        if (IS_POST) {
            $style=(new PucController())->style(I('post.style'));
            $style=json_encode($style,JSON_UNESCAPED_UNICODE);
            $data['gname']=$_POST['gname'];
            $data['number']=$_POST['number'];
            $data['gid']=$gid;
            $data['price']=$_POST['price'];
            $data['priced']=$_POST['priced'];
            $data['nid']=$_POST['nid'];
            $data['cid']=$_POST['cid'];
            $data['bid']=$_POST['bid'];
            $data['digest']=$_POST['digest'];
            $data['style']=$style;
            $data['styled']=$_POST['style'];
            $data['adduser']=$_POST['adduser'];
            $data['content']=$_POST['editorValue'];
            $data['sort']=$_POST['sort'];
            $data['thumb']=$_POST['thumb'];
            if($_POST['thumb']!=$olddata['thumb']){
                $path=$olddata['thumb'];
                $path=substr($path,5);
                $path='.'.$path;
                if(file_exists($path)){
                  unlink($path);
                }
            }
                $res=D('goods')->edit($data);
            if($res['valid']){
                D('pic')->where("gid=$gid")->delete();
                $pic['gid']=$gid;
                $pic['pic']=I('pic');
                $res1=D('pic')->add($pic);
                if($res1){
                    $this->success('修改成功',U('goods/lists/index'));die;
                }else{
                    $this->error('修改失败');die;
                }
            }else{
                $this->jump($res);
            }
            die;
        }
        $this->display();
    }

    /**
     * 上架功能
     */
    public function  putaway()
    {
        $gid = I('gid');
//        dd($gid);
        $res = M('goods')->where("gid=$gid")->setField('status', 1);
        $this->hint($res, '上架成功', '上架失败', 'index');
    }

    /**
     * 下架功能
     */
    public function  putout()
    {
        $gid = I('gid');
        $res = M('goods')->where("gid=$gid")->setField('status', 0);
        $this->hint($res, '下架成功', '下架失败', 'index');
//        dd($gid);
    }

    /**
     * 跳转提示
     */
    private function hint($res, $msg1, $msg2, $url1 = null, $url2 = null)
    {
        if ($res) {
            if (is_null($url1)) {
                $this->success($msg1, $url1);
                die;
            } else {
                $this->success($msg1, U($url1));
                die;
            }
        } else {
            if (is_null($url2)) {
                $this->error($msg2, $url2);
                die;
            } else {
                $this->error($msg2, U($url2));
                die;
            }
        }
    }

    /**
     * 删除商品
     */
    public function del(){
       $gid=I('get.gid');
       M('goods')->where("gid=$gid")->delete();
       $res=M('pic')->where("gid=$gid")->delete();
        $this->hint($res,'删除成功','删除失败','lists');
    }

    /**
     * 推荐
     */
    public function recom(){
          $gid=I('get.gid');
        M('goods')->where("gid=$gid")->setField('recom',1);
        $this->redirect('index');
//        dd($gid);die;
    }
    /**
     * 取消推荐
     */
    public function notrecom(){
        $gid=I('get.gid');
        M('goods')->where("gid=$gid")->setField('recom',0);
        $this->redirect('index');
    }
    /**
     * 热门
     */
    public function hot(){
        $gid=I('get.gid');
        M('goods')->where("gid=$gid")->setField('hot',1);
        $this->redirect('index');
    }
    /**
     * 取消热门
     */
    public function nothot(){
        $gid=I('get.gid');
        M('goods')->where("gid=$gid")->setField('hot',0);
        $this->redirect('index');
    }

    /**
     * 找出某个分类的子类
     */
    public function finson($cid, $data)
    {
        static $arr = [];
        foreach ($data as $k => $v) {
            if ($cid == $v['sid']) {
                $arr[] = $v['cid'];
                $this->finson($v['cid'], $data);
            }
        }
        return $arr;
    }
}